package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.EmpRewardPunishAddDTO;
import org.jsola.hr.dto.EmpRewardPunishUpdateDTO;
import org.jsola.hr.query.EmpRewardPunishQuery;
import org.jsola.hr.service.IEmpRewardPunishService;
import org.jsola.hr.vo.EmpRewardPunishListVO;
import org.jsola.hr.vo.EmpRewardPunishVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 员工奖惩记录
 *
 * @author zhr
 */
@RestController("hrEmpRewardPunishController")
@RequestMapping(HrConstants.API_PATH + "/v1/empRewardPunishes")
public class EmpRewardPunishController implements IEmpRewardPunishControllerAPI {

    @Autowired
    private IEmpRewardPunishService empRewardPunishService;

    @Override
    @PostMapping
    public Result<EmpRewardPunishVO> save(@Valid @RequestBody EmpRewardPunishAddDTO empRewardPunishAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工奖惩记录权限校验

        return Result.success(
                empRewardPunishService.save(empRewardPunishAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody EmpRewardPunishUpdateDTO empRewardPunishUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工奖惩记录权限校验

        int count = empRewardPunishService.update(empRewardPunishUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{empRewardPunishId}")
    public Result<Integer> delete(@PathVariable String empRewardPunishId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工奖惩记录权限校验

        int count = empRewardPunishService.deleteByIds(tokenUser, empRewardPunishId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] empRewardPunishIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工奖惩记录权限校验

        int count = empRewardPunishService.deleteByIds(tokenUser, empRewardPunishIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{empRewardPunishId}")
    public Result<EmpRewardPunishVO> findById(@PathVariable String empRewardPunishId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工奖惩记录权限校验

        return Result.success(
            empRewardPunishService.selectById(empRewardPunishId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<EmpRewardPunishListVO>> listAll(@Valid EmpRewardPunishQuery empRewardPunishQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工奖惩记录权限校验

        return Result.success(
            empRewardPunishService.select(empRewardPunishQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<EmpRewardPunishListVO>> page(@Valid EmpRewardPunishQuery empRewardPunishQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            empRewardPunishService.selectPage(empRewardPunishQuery, tokenUser.getSiteId())
        );
    }


}

